﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Collections;
using System.Data;

namespace Fjaspx.ServerControls
{
    public class WebTopRepeater : _Repeater
    {
        //私有变量
        private string mstrEmptyText = "<!--DataSource为空！-->";//DataSource为空的时候显示的字
        private IList dataSource;//数据源，必须可以转换为IList
        private int intItemCount;

        //自定义绑定
        private int _Top = 1;//显示条数 
        private string  _Class = null;//分类标识
        private string _StrWhere = "";//查询条件
        private string _FiledOrder = "bh desc";//排序规则
        private bool _TuiJian = false ;//推荐

        public int Top
        {
            get { return _Top; }
            set { _Top = value; }
        }
        public bool TuiJian
        {
            get { return _TuiJian; }
            set { _TuiJian = value; }
        }
        public string Class
        {
            get {
                if (_Class != null)
                {
                    if (Page.Request["id"] != null)
                    { _Class = _Class.Replace("{Id}".ToLowerInvariant(), Page.Request["id"]); }
                    if (Page.Request["class"] != null)
                    { _Class = _Class.Replace("{Class}".ToLowerInvariant(), Page.Request["class"]); }
                    if (Page.Request["classkind"] != null)
                    { _Class = _Class.Replace("{ClassKind}".ToLowerInvariant(), Page.Request["classkind"]); }
                }
                return _Class;
            }
            set {
                _Class = value; 
            
            }
        }
        public string StrWhere
        {
            get
            {
                if (_StrWhere != "")
                {
                    if (Page.Request["id"] != null)
                    { _StrWhere = _StrWhere.Replace("{Id}".ToLowerInvariant(), Page.Request["id"]); }
                    if (Page.Request["class"] != null)
                    { _StrWhere = _StrWhere.Replace("{Class}".ToLowerInvariant(), Page.Request["class"]); }
                    if (Page.Request["classkind"] != null)
                    { _StrWhere = _StrWhere.Replace("{ClassKind}".ToLowerInvariant(), Page.Request["classkind"]); }
                    if (Page.Request["tuijian"] != null)
                    { _StrWhere = _StrWhere.Replace("{tuijian}".ToLowerInvariant(), Page.Request["tuijian"]); }
                }
                return _StrWhere;
            }
            set
            {
                _StrWhere = value;

            }
        }
        public string FiledOrder
        {
            get
            {
                return _FiledOrder;
            }
            set
            {
                _FiledOrder = value;
            }
        }
        /**//// <summary>
        /// 数据源为空时显示的文本，默认为空
        /// </summary>
        public string EmptyText
        {
            set { mstrEmptyText = value; }
        }
        /**//// <summary>
        /// 设置 MyRepeater 控件的数据源。
        /// 可以接受 DataSet、DataTable和所以实现了IList借口的数据源。
        /// </summary>
        public override object DataSource
        {
            set
            {
                try
                {
                    if(value == null)
                    {
                        dataSource = null;
                        intItemCount = 0;
                    }
                    else if(value.GetType() == typeof(DataTable))//数据源是DataTable，转化为IList
                    {
                        dataSource = (IList)(new DataView((DataTable)value));
                        intItemCount = dataSource.Count;
                    }
                    else if(value.GetType() == typeof(DataSet))//数据源是DataSet，转化为IList
                    {
                        dataSource = (IList)(new DataView(((DataSet)value).Tables[0]));
                        intItemCount = dataSource.Count;
                    }
                    else
                    {
                        dataSource = (IList)value;
                        intItemCount = dataSource.Count;
                    }
                }
                catch
                {
                    dataSource = null;
                    intItemCount =0;
                }
            }

        }

        protected override void OnLoad(EventArgs e)
        {
            if(Visible) 
            {
                this.DataBind();
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if(intItemCount == 0)//数据源为空
            {
                writer.Write(mstrEmptyText);
                return;
            }
            base.Render(writer);
        }

        protected override void OnDataBinding(EventArgs e)
        {
            string Where = null;
            if (Class != null) 
            { 
                Where = " classname='" + Class + "'";
                if (TuiJian) { Where += " and tuijian=0 "; }
            }
            else 
            {
                Where = StrWhere;
            }

            base.DataSource = dataSource = (IList)(new DataView(((DataSet)(new Fjaspx.Sys.Bll.ROYcms_news().GetList(Top, Where, FiledOrder))).Tables[0]));
            intItemCount = dataSource.Count;
            base.OnDataBinding(e);
        }
    }
}
